import { _decorator, Button, Component, instantiate, Node } from 'cc';
import { ResMgr } from './ResMgr';
const { ccclass, property } = _decorator;


export class UIMgr extends Component {
    private Canvas: Node = null;
    public static instance: UIMgr = null;
    private uiMap = {}
    onLoad () {
        if(UIMgr.instance === null){
            UIMgr.instance = this;
        }else{
            this.destroy();
            return;
        }
        this.Canvas = this.node.parent;
    }
    public show_ui(ui_name, parent?:Node):Node{
        if(!parent){
            parent = this.Canvas;
        }
        var prefab = ResMgr.instance.getAsset("GUI", "ui_prefabs/" + ui_name);
        var item = null
        if(prefab){
            item = instantiate(prefab);
            parent.addChild(item);
            item.addComponent(ui_name + "_Ctrl")
        }
        this.uiMap[ui_name] = item;
        return item;
    }

    public remove_ui(ui_name){
        if(this.uiMap[ui_name]){
            this.uiMap[ui_name].removeFromParent();
            this.uiMap[ui_name] = null;
        }
    }

    public clearAll(){
        for(var key in this.uiMap){
            if(this.uiMap[key]){
                this.uiMap[key].removeFromParent();
                this.uiMap[key] = null;
            }
        }
    }
    
}


